#include "mainwindow.h"
#include <QtGui/QApplication>

MainWindow::MainWindow()
{
    this->setWindowTitle("Treason");
    this->setWindowIcon(QIcon(":/images/traitor-man-32.png"));
    this->setMinimumSize(500,250);

    this->trayMenu = new QMenu("Treason", this);
        QAction *quitAction = new QAction("Quit", this->trayMenu);
        quitAction->setMenuRole(QAction::QuitRole); //for macs ;D
        quitAction->setStatusTip(tr("Close treason"));
        QObject::connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

        QAction *aboutQt = new QAction("About Qt", this->trayMenu);
        aboutQt->setMenuRole(QAction::AboutQtRole);
        aboutQt->setStatusTip(tr("About Qt"));
        QObject::connect(aboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    this->trayMenu->addAction(aboutQt);
    this->trayMenu->addSeparator();
    this->trayMenu->addAction(quitAction);



    this->trayIcon = new QSystemTrayIcon(this);
    this->trayIcon->setIcon(QIcon(":/images/traitor-man-32.png"));
    this->trayIcon->setVisible(true);
    this->trayIcon->setContextMenu(this->trayMenu);
    QObject::connect(this->trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
                     this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));


    this->tabWidget = new QTabWidget(this);
    this->tabWidget->setTabsClosable(true);
    this->tabWidget->setMovable(true);
    QObject::connect(
        this->tabWidget, SIGNAL(tabCloseRequested(int)),
        this, SLOT(removeTab(int)));
    createConnection = new CreateConnection(this->tabWidget);
    this->tabWidget->addTab(this->createConnection, tr("Settings"));


    this->setCentralWidget(this->tabWidget);

    this->statusBar = new QStatusBar(this);
    this->statusBar->showMessage(tr("Ready and awaiting orders, master."));

    this->setStatusBar(this->statusBar);

    QObject::connect(
        this->createConnection, SIGNAL(newStatusBarMsg(QString)),
        this, SLOT(newStatusBarMsg(QString)));
    QObject::connect(
        this->createConnection, SIGNAL(newConnection(QTcpSocket*)),
        this, SLOT(createNewConnection(QTcpSocket*)));
}

void MainWindow::newStatusBarMsg(QString msg)
{
    this->statusBar->showMessage(msg, 3000);
}

void MainWindow::createNewConnection(QTcpSocket *socket)
{
    QString tabName = QString("%1:%2").arg(socket->peerAddress().toString()).arg(socket->peerPort());

    Connection *conn = new Connection(socket, this);
    this->tabWidget->addTab(conn, tabName);
}

void MainWindow::removeTab(int tabNum)
{
    this->tabWidget->widget(tabNum)->deleteLater();
    this->tabWidget->removeTab(tabNum);
}

void MainWindow::trayActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason)
    {
        case QSystemTrayIcon::Trigger: //single clicked
            this->setVisible(!this->isVisible());
            break;
        case QSystemTrayIcon::MiddleClick:
            this->trayIcon->showMessage(tr("Treason"), tr("Treason is a p2p chat client made in C++/Qt. We hope you find it useful"),
                                        QSystemTrayIcon::Information);
            break;
        case QSystemTrayIcon::Context:
            qDebug("Context message requested");
            break;
        default:
            qDebug("Unkown Activation Reason.");
    }
}
